package edu.pe.cayetano.wsseguridad.bo;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import ws.synopsis.frmwrk.common.BResult;
import ws.synopsis.frmwrk.common.BusinessObject;
import edu.pe.cayetano.wsseguridad.framework.DAOException;

public class BaseBO extends BusinessObject{

	private	String 			portMySQL		="3306";
	private String 			usernameMySQL	="root";
	private String 			passwordMySQL	="root";
	private String 			dbName			="seccd_all";
	protected Connection 	conn 			= null;
	private String 			connectionType	= "1";
	private String 			clasForNameDB2	= "com.ibm.db2.jcc.DB2Driver";
	private String			dataBase		="SECCD";
	private String			userDb2Casa		="db2admin";
	private String			userUnique		="db2admin";
	private String			pswdDb2Casa		="admindb2";
	private String			pswdUnique		="admindb2";
	
	public BResult conectar() throws Exception {
		BResult		retConn = new BResult();
		if(connectionType.equalsIgnoreCase("1")){
			retConn = conectar_db2_casa(dataBase,userDb2Casa,pswdDb2Casa);
		}
		if(connectionType.equalsIgnoreCase("2")){
			retConn = conectar_db2(dataBase,userUnique,pswdUnique);
		}
		if(connectionType.equalsIgnoreCase("3")){
			retConn = conectar_mysql();
		}
		if(connectionType.equalsIgnoreCase("4")){
			retConn = conectar_jelastic();
		}
	    return retConn;
	}

	public  BResult conectar_db2_casa(String dataBase,String username,String password) throws Exception {
		BResult		retConn = new BResult();
		try {
	    	Class.forName(clasForNameDB2);
	    	conn = DriverManager.getConnection("jdbc:db2://localhost:50000/"+dataBase,username,password);
	    	//conn = DriverManager.getConnection("jdbc:db2://javier-8q73txob:50000/"+dataBase,username,password);
	    	conn.setAutoCommit(false);
	    	retConn.setObject(conn);
		}catch(SQLException e){
			retConn.setObject(null);
			retConn.setCode(Integer.valueOf(e.getSQLState()).intValue());
			retConn.setMessage(e.getMessage());
			System.out.println("[WSUsuario][BaseBO][conectar_db2_casa]:error generado por errorCode:<"+e.getErrorCode()+">:SQLState<"+e.getSQLState()+">");
		}catch (Exception e) {
			System.out.println("[WSUsuarioBO][BaseBO][conectar_db2_casa] error generado por: "+e.getMessage());
		}
	    return retConn;
	}
	
	public  BResult conectar_db2(String dataBase,String username,String password) throws Exception {
	    BResult		retConn = new BResult();
		try {
	    	Class.forName(clasForNameDB2);
	    	conn = DriverManager.getConnection("jdbc:db2://localhost:50000/"+dataBase,username,password);
	    	conn.setAutoCommit(false);
	    	retConn.setObject(conn);
		}catch(SQLException e){
			retConn.setObject(null);
			retConn.setCode(e.getErrorCode());
			retConn.setMessage(e.getMessage());
			System.out.println("[WSUsuario][BaseBO][conectar_db2]:error generado por errorCode:<"+e.getErrorCode()+">:SQLState<"+e.getSQLState()+">");
		}
	    catch (Exception e) {
			System.out.println("WSUsuarioBO][BaseBO][conectar_db2] error generado por: "+e.getMessage());
			retConn.setObject(null);
			retConn.setCode(-1);
		}
	    return retConn;
	}
	
	public  BResult conectar_mysql() throws Exception {
		BResult		retConn = new BResult();
		try {
	    	Class.forName("com.mysql.jdbc.Driver");
	    	conn = DriverManager.getConnection("jdbc:mysql://localhost:"+portMySQL+"/"+dbName+"?user="+usernameMySQL+";password="+passwordMySQL);
	    	conn.setAutoCommit(false);
	    	retConn.setObject(conn);
	 	}catch(SQLException e){
			retConn.setObject(null);
			retConn.setCode(e.getErrorCode());
			retConn.setMessage(e.getMessage());
			System.out.println("[WSUsuario][BaseBO][conectar_mysql]:error generado por errorCode:<"+e.getErrorCode()+">:SQLState<"+e.getSQLState()+">");
		}catch (Exception e) {
			System.out.println("WSUsuarioBO][BaseBO][conectar_mysql]error generado por: "+e.getMessage());
		}
	    return retConn;
	}

	public  BResult conectar_jelastic() throws Exception {
		BResult		retConn = new BResult();
		try {
	        Properties prop = new Properties();
	        prop.load(new FileInputStream(System.getProperty("user.home") + "/mydb.cfg"));
	        String host = "jdbc:mysql://mysql-seccd.jelastic.servint.net/seccd"; 
	        String username = "root";
	        String password = "asyjSgDvcl";
	        String driver = "com.mysql.jdbc.Driver";
	        System.out.println(username);
	        System.out.println(password);
	        Class.forName(driver);
	        conn = DriverManager.getConnection(host, username, password);
	        conn.setAutoCommit(false);
	    	retConn.setObject(conn);
	 	}catch(SQLException e){
			retConn.setObject(null);
			retConn.setCode(e.getErrorCode());
			retConn.setMessage(e.getMessage());
			System.out.println("[WSUsuario][BaseBO][conectar_jelastic]:error generado por errorCode:<"+e.getErrorCode()+">:SQLState<"+e.getSQLState()+">");
		}catch (Exception e) {
			System.out.println("WSUsuarioBO][BaseBO][conectar_jelastic]error generado por: "+e.getMessage());
		}
	    return retConn;
	}

	public void grabar() throws DAOException {
		try{
			if(conn != null){
				conn.commit();
			}
		}catch (Exception e){
			throw new DAOException(e.getMessage(), e);
		}
	}

	public void deshacer() throws Exception {
		try{
			if(conn != null) {
			conn.rollback();
			}
		}catch (Exception e){
			throw new DAOException(e.getMessage(), e);
		}
	}
	
	public static String intArrayToString(int[] arreglo){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<arreglo.length;i++){
			sb.append(arreglo[i]).append(",");
		}
		if(sb.length() > 0)
			sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}
	
	public void desconectar() throws DAOException {
		try{
			if(conn != null) {
			    conn.setAutoCommit(true);
			    conn.close();
			}
		}catch (Exception e){
			throw new DAOException(e.getMessage(), e);
		}
	}
}
